package com.ss.android.download;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.text.format.Formatter;
import android.util.Log;

import com.ss.android.article.browser.R;
import com.ss.android.common.download.d;
import com.ss.android.common.download.e;

import java.util.LinkedList;
import java.util.Queue;

public class SizeLimitActivity extends Activity implements DialogInterface.OnClickListener {
    private d a;
    private Queue b;
    private Uri c;
    private Intent d;

    public SizeLimitActivity() {
        this.b = new LinkedList();
    }

    private void a() {
        e v4;
        String v2_1;
        String v0_2;
        String[] v2 = null;
        if (this.a != null) {
            return;
        }

        if (this.b.isEmpty()) {
            this.finish();
            return;
        }

        this.d = (Intent) this.b.poll();
        this.c = this.d.getData();
        if (this.c == null) {
            this.b();
            return;
        }

        Cursor v1 = k.a(this.getApplicationContext()).a(this.c, v2, null, v2, null);
        try {
            if (v1.moveToFirst()) {
                v0_2 = Formatter.formatFileSize(((Context) this), ((long) v1.getInt(v1.getColumnIndexOrThrow("total_bytes"))));
                v2_1 = this.getString(com.ss.android.article.browser.R.string.ek);
                boolean v3 = this.d.getExtras().getBoolean("isWifiRequired");
                v4 = android.support.design.a.v(((Context) this));
                if (v3) {
                    v4.a(com.ss.android.article.browser.R.string.wifi_required_title).a(this.getString(com.ss.android.article.browser.R.string.wifi_required_body, new Object[]{v0_2, v2_1})).a(com.ss.android.article.browser.R.string.ek, ((DialogInterface.OnClickListener) this)).b(com.ss.android.article.browser.R.string.ej, ((DialogInterface.OnClickListener) this));
                } else {
                    int v3_1 = R.string.wifi_recommended_title;
                    v4.a(v3_1).a(this.getString(com.ss.android.article.browser.R.string.wifi_recommended_body, new Object[]{v0_2, v2_1})).a(com.ss.android.article.browser.R.string.el, ((DialogInterface.OnClickListener) this)).b(com.ss.android.article.browser.R.string.ek, ((DialogInterface.OnClickListener) this));
                }

                this.a = v4.a(new q(this)).a();
                if (v1 == null) {
                    return;
                }
            }

            Log.e("SsDownloadManager", "Empty cursor for URI " + this.c);
            this.b();
            if (v1 == null) {
                return;
            }
        } catch (Exception v0) {
            try {
                v1.close();
            } catch (Exception ex) {
            }
        }
    }

    private void b() {
        this.a = null;
        this.c = null;
        this.a();
    }

    public void onClick(DialogInterface arg5, int arg6) {
        String v3 = null;
        boolean v0 = this.d.getExtras().getBoolean("isWifiRequired");
        if (v0) {
            android.support.design.a.o();
            if (arg6 == -2) {
                k.a(this.getApplicationContext()).a(this.c);
            } else {
                if (!v0) {
                    android.support.design.a.o();
                    if (arg6 == -1) {
                        ContentValues v0_1 = new ContentValues();
                        v0_1.put("bypass_recommended_size_limit", Boolean.valueOf(true));
                        k.a(this.getApplicationContext()).a(this.c, v0_1, v3, null);
                    }
                }
            }
        } else {
            if (!v0) {
                android.support.design.a.o();
                if (arg6 == -1) {
                    ContentValues v0_1 = new ContentValues();
                    v0_1.put("bypass_recommended_size_limit", Boolean.valueOf(true));
                    k.a(this.getApplicationContext()).a(this.c, v0_1, v3, null);
                }
            }
        }

        this.b();
    }

    protected void onNewIntent(Intent arg1) {
        super.onNewIntent(arg1);
        this.setIntent(arg1);
    }

    protected void onResume() {
        super.onResume();
        Intent v0 = this.getIntent();
        if (v0 != null) {
            this.b.add(v0);
            this.setIntent(null);
            this.a();
        }

        if (this.a != null) {
            d v0_1 = this.a;
            boolean v0_2 = v0_1.a != null ? v0_1.a.isShowing() : false;
            if (v0_2) {
                return;
            }

            v0_1 = this.a;
            if (v0_1.a == null) {
                return;
            }
            v0_1.a.show();
        }
    }
}

